package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysMenuService;
import com.atguigu.spzx.model.pojo.system.SysMenu;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.utils.DYLAssert;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "菜单管理")
@RestController
@RequestMapping("/user/system/sysMenu")
@CrossOrigin(allowCredentials = "true", originPatterns = "*", allowedHeaders = "*")
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;

    @GetMapping("/findMenuTree")
    @Operation(summary = "获取菜单树")
    public Result<List<SysMenu>> findMenuTree() {
        List<SysMenu> sysMenus = sysMenuService.findMenuTree();
        return Result.ok(sysMenus);
    }

    @PostMapping("/addMenu")
    @Operation(summary = "添加菜单")
    public Result addMenu(@RequestBody SysMenu sysMenu) {
        DYLAssert.notNull(sysMenu, "添加失败。。。");
        DYLAssert.isTrue(sysMenuService.save(sysMenu), "添加失败。。。");
        return Result.ok();
    }

    @PutMapping("/updateMenu")
    @Operation(summary = "修改菜单")
    public Result updateMenu(@RequestBody SysMenu sysMenu) {
        DYLAssert.notNull(sysMenu, "修改失败。。。");
        DYLAssert.isTrue(sysMenuService.updateById(sysMenu), "修改失败。。。");
        return Result.ok();
    }

    @DeleteMapping("/deleteMenu/{id}")
    @Operation(summary = "删除菜单")
    public Result deleteMenu(@PathVariable("id") Long id) {
        DYLAssert.notNull(id, "删除失败，id为空");
        DYLAssert.isTrue(sysMenuService.doDeleteMenu(id), "删除失败。。。");
        return Result.ok();
    }
}
